package com.zzq.my.jetpacks.jectpackapp.action

import `in`.srain.cube.actionqueque.ActionQueue
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface


/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2022/4/24-15:33
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class PopDialogAction(
    private val ctx: Context,
    msg: String,
    listenerBuilder: ListenerBuilder.() -> Unit
) : ActionQueue.Action<String>(msg) {

    //todo 3 利用lazy 的的方法
    val mListener:ListenerBuilder by lazy { ListenerBuilder().also(listenerBuilder) }
    //todo 2 利用init 的方法
//    private var mListener: ListenerBuilder
//    init {
//        mListener = ListenerBuilder().also(listenerBuilder)
//    }


//    private lateinit var mListener: StateLiveData.ListenerBuilder
//    //todo 1 注册接口回调
//    fun registerListener(listenerBuilder: ListenerBuilder.()->Unit) {
//        mListener = ListenerBuilder().also(listenerBuilder)
//    }

    override fun onAction() {
        val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
        val dialog: Dialog = builder.setTitle("温馨提示").setPositiveButton("确定") { dialogInterface, _ ->
            mListener.onDismissListenerFun(dialogInterface)
        }.setMessage(badge).show()
        dialog.setCancelable(false)
        // notify action is done, and next aciton will be executed
        //todo action 执行完之后通知提醒：
        dialog.setOnDismissListener(mOnDismissListener);
    }

    private var mOnDismissListener =
        DialogInterface.OnDismissListener {
            mListener.onDismissListenerFun(it)
//            mActionQueue.notifyActionDoneThenTryToPopNext()

        }


    inner class ListenerBuilder {
        private var mOnDismissListener: (listener: DialogInterface?) -> Unit = {}

        fun onDismissListenerFun(params: DialogInterface?) {
            this.mOnDismissListener.invoke(params)
        }
    }

}